package com.junxonline.instant.terminal.config;

import com.junxonline.instant.common.constant.RedisMqChannelConstant;
import com.junxonline.instant.terminal.listener.TerminalConnectListener;
import com.junxonline.instant.terminal.listener.TerminalStatusListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * Redis消息配置类
 *
 * @author JunX
 * @date 2021-11-10
 */
@Configuration
public class RedisMqConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter ...listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 添加订阅频道
        container.addMessageListener(listenerAdapter[0], new PatternTopic(RedisMqChannelConstant.KEY_REDIS_MSG_CHANNEL_TERMINAL_STATUS));
        container.addMessageListener(listenerAdapter[1], new PatternTopic(RedisMqChannelConstant.KEY_REDIS_MSG_CHANNEL_LOG_DEVICE_CONNECT));
        return container;
    }

    @Bean
    public MessageListenerAdapter terminalStatusListenerAdapter(TerminalStatusListener terminalStatusListener) {
        return new MessageListenerAdapter(terminalStatusListener);
    }

    @Bean
    public MessageListenerAdapter terminalConnectListenerAdapter(TerminalConnectListener terminalConnectListener) {
        return new MessageListenerAdapter(terminalConnectListener);
    }

    @Bean
    public StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

}
